﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VNAManagebank.Common;
using VNAManagebank.Bizlogic;
using VNAManagebank.Entity;

namespace VNAManagebank
{
    public partial class frmNhanVien : Form
    {
        public frmNhanVien()
        {
            InitializeComponent();
        }

        private void frmNhanVien_Load(object sender, EventArgs e)
        {
            try
            {
                HienThi();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
            }
        }

        private void HienThi()
        {
            dataGridView1.DataSource = csdl.hienthi("select * from NhanVien");
            FiXDatagridview();
        }

        void FiXDatagridview()
        {
            dataGridView1.AllowUserToResizeRows = false;
            dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            dataGridView1.ReadOnly = true;

            for (int j = 0; j < dataGridView1.ColumnCount; j++)
            {
                dataGridView1.Columns[j].Visible = false;
            }

            dataGridView1.Columns[comNhanVien.MaNhanVien].Visible = true;
            dataGridView1.Columns[comNhanVien.MaNhanVien].HeaderText = "Mã Nhân Viên";
            dataGridView1.Columns[comNhanVien.TenNhanVien].Visible = true;
            dataGridView1.Columns[comNhanVien.TenNhanVien].HeaderText = "Tên Nhân Viên";
            dataGridView1.Columns[comNhanVien.GioiTinh].Visible = true;
            dataGridView1.Columns[comNhanVien.GioiTinh].HeaderText = "Giới Tính";
            dataGridView1.Columns[comNhanVien.NgaySinh].Visible = true;
            dataGridView1.Columns[comNhanVien.NgaySinh].HeaderText = "Ngày Sinh";
            dataGridView1.Columns[comNhanVien.SCMND].Visible = true;
            dataGridView1.Columns[comNhanVien.SCMND].HeaderText = "Số CMTND";
            dataGridView1.Columns[comNhanVien.DiaChi].Visible = true;
            dataGridView1.Columns[comNhanVien.DiaChi].HeaderText = "Địa Chỉ";
            dataGridView1.Columns[comNhanVien.SoDienThoai].Visible = true;
            dataGridView1.Columns[comNhanVien.SoDienThoai].HeaderText = "Số Điện Thoại";

            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
            dataGridView1.AllowUserToResizeRows = false;
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                DataGridViewRow dgvRow = dataGridView1.Rows[e.RowIndex];
                txtMaNhanVien.Text = dgvRow.Cells["MaNhanVien"].Value.ToString();
                txtTenNhanVien.Text = dgvRow.Cells["TenNhanVien"].Value.ToString();
                string gioitinh = dgvRow.Cells["GioiTinh"].Value.ToString();
                if (gioitinh.Equals("Nam"))
                {
                    rdoNam.Checked = true;
                    rdoNu.Checked = false;
                }
                else
                {
                    rdoNam.Checked = false;
                    rdoNu.Checked = true;
                }
                txtNgaySinh.Text = dgvRow.Cells["NgaySinh"].Value.ToString();
                txtSoCMTND.Text = dgvRow.Cells["SCMND"].Value.ToString();
                txtDiaChi.Text = dgvRow.Cells["DiaChi"].Value.ToString();
                txtDienThoai.Text = dgvRow.Cells["SoDienThoai"].Value.ToString();
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                //Check validate
                if (string.IsNullOrEmpty(txtMaNhanVien.Text))
                {
                    MessageBox.Show("Bạn chưa nhập mã nhân viên", MSG.TieuDe, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtMaNhanVien.Focus();
                    return;
                }

                int kq = csdl.themsuaxoa("insert into NhanVien(MaNhanVien,TenNhanVien, GioiTinh, NgaySinh, SCMND, DiaChi, SoDienThoai) values(N'" + txtMaNhanVien.Text + "',N'" + txtTenNhanVien.Text + "',N'" + (rdoNam.Checked ? "Nam" : "Nữ") + "',N'" + txtNgaySinh.Text + "',N'" + txtSoCMTND.Text + "',N'" + txtDiaChi.Text + "',N'" + txtDienThoai.Text + "')");
                HienThi();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                //Check validate
                if (string.IsNullOrEmpty(txtMaNhanVien.Text))
                {
                    MessageBox.Show("Bạn chưa nhập mã nhân viên", MSG.TieuDe, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtMaNhanVien.Focus();
                    return;
                }

                int kq = csdl.themsuaxoa("delete from NhanVien where MaNhanVien = '" + txtMaNhanVien.Text + "'");
                HienThi();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                //Check validate
                if (string.IsNullOrEmpty(txtMaNhanVien.Text))
                {
                    MessageBox.Show("Bạn chưa nhập mã nhân viên", MSG.TieuDe, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtMaNhanVien.Focus();
                    return;
                }

                int kq = csdl.themsuaxoa("update NhanVien set TenNhanVien = N'" + txtTenNhanVien.Text + "', GioiTinh = N'" + (rdoNam.Checked ? "Nam" : "Nữ") + "', NgaySinh = N'" + txtNgaySinh.Text + "', SCMND = N'" + txtSoCMTND.Text + "', DiaChi = N'" + txtDiaChi.Text + "', SoDienThoai = N'" + txtDienThoai.Text + "' Where  MaNhanVien = '" + txtMaNhanVien.Text + "'");
                HienThi();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
            }
        }
    }
}
